﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InnovTech.DynamicData
{
    public class CommonDynamicData : IDynamicData
    {

        private List<IDynamicObject> _objects;

        public CommonDynamicData(string name)
        {
            DataName = name;
        }

        public ICollection<IDynamicObject> Objects
        {
            get { if (_objects == null) _objects = new List<IDynamicObject>(); return _objects; }
        }

        public string DataName
        {
            get;
            private set;
        }
    }

    public class CommonDynamicObject : IDynamicObject
    {

        private Dictionary<string, IDynamicObjectMember> _members;

        public CommonDynamicObject(string name, Type type)
        {
            ObjectName = name;
            ObjectType = type;
        }

        public Type ObjectType
        {
            get;
            private set;
        }

        public string ObjectName
        {
            get;
            private set;
        }

        public IDictionary<string, IDynamicObjectMember> Members
        {
            get { if (_members == null) _members = new Dictionary<string, IDynamicObjectMember>(); return _members; }
        }

        public IEnumerable<string> MemberNames
        {
            get { return Members.Keys; }
        }
    }

    public class CommonDynamicObjectMember : IDynamicObjectMember
    {

        public CommonDynamicObjectMember(string name, Type type, IDynamicObject host = null, string path = null)
        {
            MemberName = name;
            MemberType = type;

            // if the host object is null --> the member is a temp value and does not belong to any domain object.
            HostObject = host;
            MemberPath = path ?? MemberName;
        }

        public string MemberPath
        {
            get;
            private set;
        }

        public Type MemberType
        {
            get;
            private set;
        }

        public string MemberName
        {
            get;
            private set;
        }

        public object MemberValue
        {
            get;
            //throws exception when the type is not ok
            set;
        }

        public IDynamicObject HostObject
        {
            get;
            private set;
        }
    }


}
